package com.tedu.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.tedu.dao.ICartDao;
import com.tedu.model.Cart;
import com.tedu.service.ICartService;
@Service
public class CarService implements ICartService {
	private ICartDao cartDao;
	
	@Resource
	public void setCartDao(ICartDao cartDao) {
		this.cartDao = cartDao;
	}
	
	@Override
	public void add(Cart cart) {
		//创建一个Map集合,用来查询同一个用户的购物车有没有相同的商品
		Map<String, Integer> map = new HashMap<String, Integer>();
		map.put("pid", cart.getProduct().getId());
		map.put("uid", cart.getUser().getId());
		Cart c = cartDao.loadByProduct(map);
		//购物车中没有该条记录
		if(c == null) {
			cartDao.add(cart);
		}
		//购物车中有该条数据
		else {
			//修改购物车记录中的商品数量
			c.setQuantity(cart.getQuantity() + c.getQuantity());
			cartDao.update(c);
		}
	}
	
	public void delete(int id) {
		cartDao.delete(id);
	}

	@Override
	public int cartSize(int uid) {
		// TODO Auto-generated method stub
		int cartSeize = cartDao.cartSize(uid);
		return cartSeize;
	}

	@Override
	public List<Cart> list(int uid) {
		List<Cart> carts = cartDao.list(uid);
		return carts;
	}

	@Override
	public Cart load(int id) {
		// TODO Auto-generated method stub
		return cartDao.load(id);
	}

	/**
	 * 修改购物车中的商品数量
	 */
	@Override
	public void update(Cart cart) {
		// TODO Auto-generated method stub
		cartDao.update(cart);
	}

}
